import CozeClientClass from "../utils/cozeClient.mjs";
import {RoleType} from "@coze/api";

class CozeModelClass{
    static _instance=null;
    constructor(token) {
        // this.token=token
        // console.log("123456");
        this.client=CozeClientClass.getInstance(token);
        // 绑定方法上下文
        this.chatStream = this.chatStream.bind(this);
    }
    // 静态获取实例方法
    static getCozeModelInstance(token) {

        if (!CozeModelClass._instance) {
            CozeModelClass._instance = new CozeModelClass(token);

        }
        return CozeModelClass._instance;
    }
    async chatStream(info) {
        try {
            //异步生成器
            return await this.client.chat.stream({
                bot_id: process.env.COZE_BOT_ID,
                additional_messages: [
                    {
                        role: RoleType.User, // 假设RoleType已定义
                        content: info.content,
                        content_type: info.contentType,
                    },
                ],
            });

            // let fullResponse = '';
            // for await (const part of stream) {
            //     if (part.event === ChatEventType.CONVERSATION_MESSAGE_DELTA) { // 假设ChatEventType已定义
            //         const content=part.data.content
            //         fullResponse +=  content;
            //         process.stdout.write(content);
            //
            //     }
            //     if(part.event===ChatEventType.CONVERSATION_CHAT_COMPLETED){
            //         // const
            //     }
            // }
            // return fullResponse;

        } catch (error) {
            console.error('Chat stream error:', error);
            throw new Error(`Chat failed: ${error.message}`);
        }
    }
}

export default CozeModelClass;


